package com.mcptt.config.body.info.mcdata.binary;

import com.cloudptt.api.product.log.Log;

public class BinaryBody {
    protected static final byte Type_Text = 0x1;//TEXT：1
    protected static final byte Type_Binary = 0x2;//BINARY： 2
    protected static final byte Type_Hyperlink = 0x3;//HYPERLINKS：3
    protected static final byte Type_FileURL = 0x4;//FILEURL：4（URL标准可参照RFC 1738）
    protected static final byte Type_Location = 0x5;//  LOCATION：5

    public static final byte SDS_SIGNALLING_PAYLOAD = 0x1;
    public static final byte FD_SIGNALLING_PAYLOAD = 0x2;
    public static final byte DATA_PAYLOAD = 0x3;
    public static final byte SDS_NOTIFYCATION = 0x5;
    public static final byte FD_NOTIFICATION = 0x6;
    public static final byte SDS_OFF_NETWORK_MSG = 0x7;
    public static final byte SDS_OFF_NETWORK_NOTIFICATION = 0x8;
    public static final byte FD_NETWORK_NOTIFICATION = 0x9;
    public static final byte COMMUNACATION_RELEASE = 0x10;

    protected static final byte IE_REPLAY_TO_MSG_ID = 0x21;//TV
    protected static final byte IE_APPLICATION_ID = 0x22;//TV
    protected static final byte IE_SDS_DISPOSITION_REQ_TYPE = (byte) 0x80;//TV 0x8x
    protected static final byte IE_FD_DISPOSITION_REQ_TYPE =  (byte)0x90;//TV 0x9x
    protected static final byte IE_MANDARY_DOWNLOAD =  (byte)0xA0;//TV 0xAx
    protected static final byte IE_DATA_QUERY_TYPE =  (byte)0xB0;//TV 0xBx
    protected static final byte IE_EXTENSION_RSP_TYPE =  (byte)0xC0;//TV 0xCx
    protected static final byte IE_PAYLOAD = 0x78;//TLV
    protected static final byte IE_METADATA = 0x79;//TLV
    protected static final byte IE_SECURITY_PAYLOAD =  (byte)0x80;//TLV
    protected static final byte IE_PAYLOAD_COMPATIBLE = 0x4e;// TLV
    protected static final byte IE_METADATA_COMPATIBLE = 0x4f; // TLV

    public BinaryBody()
    {}

    protected static byte[] intToByteArray(int intValue)
    {
        byte[] byteArray = new byte[2]; // 创建一个2字节的数组
        // 将int值的高8位（高字节）放入数组的第一个元素
        byteArray[0] = (byte)(intValue >> 8);
        // 将int值的低8位（低字节）放入数组的第二个元素
        byteArray[1] = (byte)intValue;
        return  byteArray;
    }
    protected static int byteArrToInt(byte[] bytes)
    {
        int value = ((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF);
        return value;
    }

    protected static byte[] longToByteArray(long value) {
        byte[] byteArray = new byte[5];
        for (int i = 0; i < byteArray.length; i++) {
            int shift = (byteArray.length - i - 1) * 8;
            byteArray[i] = (byte) (value >>> shift);
        }
        return byteArray;
    }


    public static long byteArrayToLong(byte[] byteArray) {
        // 使用ByteBuffer
        Log.debug(SdsSignalBody.class,"byteArrayToLong byteArray =  " ,byteArray);
        // 手动转换（大端序）
        long valueManual = 0;
        for (int i = 0; i < byteArray.length; i++) {
            valueManual |= ((long) byteArray[i] & 0xFF) << (8 * (byteArray.length - 1 - i));
        }
        return valueManual;
    }


    protected static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i+1), 16));
        }

        // 打印byte数组
        for (byte b : data) {
            System.out.format("0x%02X ", b);
        }
        return data;
    }

    public static String byteArrayToHexStr (byte[] byteArray) {
        StringBuilder hexBuilder = new StringBuilder();
        for (byte b : byteArray) {
            hexBuilder.append(String.format("%02X", b));
        }
        String hexString = hexBuilder.toString();
        return hexString;
    }
}
